# 装饰器的本质是一个函数，把被装饰的函数当成参数
import time


# 示例1
def timer(func):
    def inner(*args, **kwargs):
        start = time.time()
        # 调用原函数并返回结果
        result = func(*args, **kwargs)
        print(time.time() - start)
        return result

    return inner


@timer
def hello_test():
    for i in range(10000):
        print(123)


hello_test()


# 示例2
def decorator(regular):
    def wrapper():
        print("wrapper function executed")
        return regular()

    return wrapper


def regular():
    print("regular function executed")


# 2.1 装饰器调用
regular_decorator = decorator(regular)
regular_decorator()


# 2.2 装饰器调用,加上注解
@decorator
def regular2():
    print("regular2 function executed")


regular2()
